package com.wx.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.wx.constant.MessageConstant;
import com.wx.entity.PageResult;
import com.wx.entity.QueryPageBean;
import com.wx.entity.Result;
import com.wx.pojo.CheckGroup;
import com.wx.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pages/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService service;
    @RequestMapping("findall")
    public Result findAll(@RequestBody QueryPageBean queryPageBean ){
        try {
            if(queryPageBean.getQueryString()==null){
                queryPageBean.setQueryString("");
            }
            PageResult allGroup = service.findAllGroup(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,allGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/add")
    public Result addGroup(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            service.addGroup(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result deleteGroup(Integer id){
        try {
            service.deleteGroupById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/update")
    public Result updaCheckgroup(Integer id){
        // 数据回显显示编辑窗口
        // 1. 查询出所有的检查项
        // 2.查询出传递的id对应的检查项
        // 3.查询出id对应的检查组的信息
        try {
            CheckGroup groupById = service.findGroupById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,groupById);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
    @RequestMapping("/findItems")
    public Result findItems(Integer id){
        try {
            String[] itemsById = service.findItemsById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,itemsById);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
    @RequestMapping("/modify")
    public Result modify(@RequestBody CheckGroup checkGroup,Integer[] items){
        try {
            service.modify(checkGroup,items);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
}
